<template>
  <a-drawer
    title="修改按钮"
    :maskClosable="false"
    width=650
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="zoneEditVisiable"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-form-item label='地区名称' v-bind="formItemLayout">
        <a-input v-decorator="['zoneName',
                   {rules: [
                    { required: true, message: '地区名称不能为空'},
                    { max: 20, message: '长度不能超过20个字符'}
                  ]}]"/>
      </a-form-item>
      <a-form-item label='地区排序' v-bind="formItemLayout">
        <a-input-number v-decorator="['zoneNum']" style="width: 100%"/>
      </a-form-item>
      <a-form-item label='上级地区'
                   style="margin-bottom: 2rem"
                   v-bind="formItemLayout">
        <a-tree
          :key="zoneTreeKey"
          :checkable="true"
          :checkStrictly="true"
          @check="handleCheck"
          @expand="handleExpand"
          :expandedKeys="expandedKeys"
          :defaultCheckedKeys="defaultCheckedKeys"
          :treeData="zoneTreeData">
        </a-tree>
      </a-form-item>
      <a-form-item label='区域瞄点' v-bind="formItemLayout">
        <a-input v-decorator="['points']"/>
      </a-form-item>
      <p>
          polygonPath: [
        {lng: 116.412732, lat: 39.911707},
        {lng: 116.39455, lat: 39.910932},
        {lng: 116.403461, lat: 39.921336}
      ]
      </p>
    </a-form>
    <div class="drawer-bootom-button">
      <a-popconfirm title="确定放弃编辑？" @confirm="onClose" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-drawer>
</template>
<script>
const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}
export default {
  name: 'ZoneEdit',
  props: {
    zoneEditVisiable: {
      default: false
    }
  },
  data () {
    return {
      loading: false,
      formItemLayout,
      form: this.$form.createForm(this),
      zoneTreeKey: +new Date(),
      zone: {},
      checkedKeys: [],
      expandedKeys: [],
      defaultCheckedKeys: [],
      zoneTreeData: []
    }
  },
  methods: {
    reset () {
      this.loading = false
      this.zoneTreeKey = +new Date()
      this.expandedKeys = this.checkedKeys = this.defaultCheckedKeys = []
      this.button = {}
      this.form.resetFields()
    },
    onClose () {
      this.reset()
      this.$emit('close')
    },
    handleCheck (checkedKeys) {
      this.checkedKeys = checkedKeys
    },
    handleExpand (expandedKeys) {
      this.expandedKeys = expandedKeys
    },
    setFormValues ({...zone}) {
      this.form.getFieldDecorator('zoneName')
      this.form.setFieldsValue({'zoneName': zone.text})
      this.form.getFieldDecorator('zoneNum')
      this.form.setFieldsValue({'zoneNum': zone.order})
      this.form.getFieldDecorator('ponits')
      this.form.setFieldsValue({'points': zone.value})
      if (zone.parentId !== '0') {
        this.defaultCheckedKeys.push(zone.parentId)
        this.checkedKeys = this.defaultCheckedKeys
        this.expandedKeys = this.checkedKeys
      }
      this.zone.zoneId = zone.id
    },
    handleSubmit () {
      let checkedArr = Object.is(this.checkedKeys.checked, undefined) ? this.checkedKeys : this.checkedKeys.checked
      if (checkedArr.length > 1) {
        this.$message.error('最多只能选择一个上级地区，请修改')
        return
      }
      if (checkedArr[0] === this.zone.zoneId) {
        this.$message.error('不能选择自己作为上级地区，请修改')
        return
      }
      this.form.validateFields((err, values) => {
        if (!err) {
          this.loading = true
          let zone = this.form.getFieldsValue()
          zone.parentId = checkedArr[0]
          if (Object.is(zone.parentId, undefined)) {
            zone.parentId = 0
          }
          zone.zoneId = this.zone.zoneId
          this.$put('zone', {
            ...zone
          }).then(() => {
            this.reset()
            this.$emit('success')
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  },
  watch: {
    zoneEditVisiable () {
      if (this.zoneEditVisiable) {
        this.$get('zone').then((r) => {
          this.zoneTreeData = r.data.rows.children
          this.zoneTreeKey = +new Date()
        })
      }
    }
  }
}
</script>
